package com.qcby.controller;

import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qcby.Constants;
import com.qcby.dao.MovieCommentMapper;
import com.qcby.dao.TicketOrderMapper;
import com.qcby.exception.MyRuntimeException;
import com.qcby.model.dto.CommentForCreateDto;
import com.qcby.model.dto.MovieCommentQueryDto2;
import com.qcby.model.dto.MovieCommentShowVo2;
import com.qcby.model.dto.MovieCommentShowVo3;
import com.qcby.model.resp.RespEntity;
import com.qcby.service.MovieCommentService;
import com.qcby.service.MovieService;
import com.qcby.util.Page;

@RestController
@RequestMapping("/comment")
public class MovieCommentController {
    @Autowired
    private MovieCommentService movieCommentService;
    @Autowired
    private MovieCommentMapper movieCommentMapper;
    @Autowired
    private TicketOrderMapper ticketOrderMapper;
    @Autowired
    private MovieService movieService;

    @RequestMapping("/list")
    public RespEntity<Page<MovieCommentShowVo2>> searchComment(@RequestBody MovieCommentQueryDto2 queryDto2) {
        if (queryDto2 != null) {
            if (queryDto2.getPageNo() == null) {
                queryDto2.setPageNo(1);
            }
            Page<MovieCommentShowVo2> page = movieCommentService.selectCommentByUid(queryDto2.getUid(),
                    queryDto2.getPageNo(), queryDto2.getPageSize(), queryDto2.getOrderByCondition());
            return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", page);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "查询失败", null);
    }


    @RequestMapping("/admin/list")
    public RespEntity<Page<MovieCommentShowVo3>> adminQuery(@RequestBody MovieCommentQueryDto2 queryDto2) {
        if (queryDto2 != null) {
            if (queryDto2.getPageNo() == null) {
                queryDto2.setPageNo(1);
            }
            Page<MovieCommentShowVo3> page = movieCommentService.selectCommentAdmin(queryDto2.getUid(), queryDto2.getMid(), queryDto2.getPageNo(), queryDto2.getPageSize(), queryDto2.getOrderByCondition());
            return RespEntity.successCustomize(Constants.FLAG_200, "查询电影成功", page);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "查询失败", null);
    }

    /**
     * 删除评论的操作
     */
    @RequestMapping("/deleteComment")
    public RespEntity<Boolean> deleteComment(@RequestParam Integer mcId) {
        if (mcId != null) {
            movieCommentService.deleteByPrimaryKey(mcId);
            return RespEntity.successCustomize(Constants.FLAG_200, "删除成功", true);
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "删除失败", false);
    }

    @RequestMapping("/insert")
    public RespEntity<String> insertComment(@RequestBody CommentForCreateDto createDto) {
        if (!createDto.isParamValid()) {
            throw new IllegalArgumentException("参数存在空值！");
        }

        Integer paidOrderNum = ticketOrderMapper.selectByUidAndMid(createDto.getUid(), createDto.getMid());
        if (paidOrderNum == 0) {
            throw new IllegalArgumentException("您没有购买过此电影的订单，无法评论！");
        }

        Integer mcId = movieCommentMapper.insertCommon(createDto.getUid(), createDto.getMid(),
                createDto.getCommentContent(), new Timestamp(System.currentTimeMillis()), createDto.getCommentScore());

        if (mcId == null) {
            throw new MyRuntimeException(Constants.FLAG_500, "创建评论失败！");
        }

        return RespEntity.successNoData("创建评论成功！您可以点击右上角头像 → 查看评论查看您发表所有的评论！");

    }
}
